/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication5.model;

import java.util.ArrayList;
import java.util.List;
import javaapplication5.model.Contact;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author mathieubakrim
 */
public class ContactsTable extends AbstractTableModel{
    
    private  List<Contact> contacts = new ArrayList<Contact>();
    
    //private  String[] titles;
    private String[] titles = {"lastName", "firstName", "phoneNumber", "addressPro", 
        "addressPer", "homePage", "emailPro", "emailPer", "group"}; 
    
    public ContactsTable(String[] titles, List<Contact> contacts ){
        super();
                
        this.titles = titles;         
        this.contacts = contacts;        
    }

    public String[] getTitles() {
        return titles;
    }

    public List<Contact> getContacts() {
        return contacts;
    }
    
    // Cant change a contact last name or firstname, otherwize you can
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex){
        if(columnIndex == 0 || columnIndex == 1) return false;
        return true;
    }
         
    @Override
    public int getRowCount() {
        return contacts.size();
    }

    @Override
    public int getColumnCount() {
        return titles.length;
    }
    
    public String getColumName(int columnIndex){
        return titles[columnIndex];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0:
                    return contacts.get(rowIndex).getLastName();
            case 1:
                    return contacts.get(rowIndex).getFirstName(); 
            case 2:
                    return contacts.get(rowIndex).getPhoneNumber();
            case 3:
                    return contacts.get(rowIndex).getAddressPro();
            case 4:
                    return contacts.get(rowIndex).getAddressPer();
            case 5:
                    return contacts.get(rowIndex).getHomePage();
            case 6:
                    return contacts.get(rowIndex).getEmailPro();
            case 7:
                    return contacts.get(rowIndex).getEmailPer();
            case 8: 
                    return contacts.get(rowIndex).getGroup();
            default:
                    return null;
        }
    }
    
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if(aValue != null){
        Contact contact = contacts.get(rowIndex);
 
        switch(columnIndex){
            case 2:
                contact.setPhoneNumber((String)aValue);
                break;
            case 3:
                contact.setAddressPro((String)aValue);
                break;
            case 4:
                contact.setAddressPer((String)aValue);
                break;
            case 5:
                contact.setHomePage((String)aValue);
                break;
            case 6:
                contact.setEmailPro((String)aValue);
                break;
            case 7:
                contact.setEmailPer((String)aValue);
                break;
            case 8:
                contact.setGroup((String)aValue);
        }
    }
}
    
    public void addContact(Contact contact){
        //contacts.add(contact);
        
        fireTableRowsInserted(contacts.size()-1, contacts.size()-1);
    }
    
    public void removeContact(int rowIndex){
        contacts.remove(rowIndex);
        
        fireTableRowsDeleted(rowIndex, rowIndex);
    }
    
}
